package com.modemMgr.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.modemMgr.dao.DepartmentDAO;
import com.modemMgr.dao.UserDAO;
import com.modemMgr.forms.TreeForm;
import com.modemMgr.util.AppUtils;
import com.modemMgr.util.Constants;
import com.modemMgr.util.DBAccessor;
import com.modemMgr.util.DBServer;
import com.modemMgr.vo.Department;
import com.modemMgr.vo.User;

public class TreeAction extends DispatchAction
{
	public ActionForward getTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		TreeForm myForm = (TreeForm)form;
		request.setAttribute("sourceType", myForm.getSourceType());
		
		if (Constants.WAREHOUSE_TYPE_ORG.equals(myForm.getSourceType()))
		{
			DBAccessor dbAccessor = DBServer.newDBAccessor(true);
			DepartmentDAO departmentDAO = new DepartmentDAO(dbAccessor);
			Department department = departmentDAO.getDepartmentById(Integer.parseInt(myForm.getSourceId()), AppUtils.getFieldWorkId());
			request.setAttribute("department", department);
		}
		else
		{
			DBAccessor dbAccessor = DBServer.newDBAccessor(true);
			UserDAO userDAO = new UserDAO(dbAccessor);
			User user = userDAO.getUserById(Integer.parseInt(myForm.getSourceId()));
			request.setAttribute("user", user);
		}
		
		return mapping.findForward("tree");
	}
	
	public ActionForward getChildNode(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		TreeForm myForm = (TreeForm)form;
		String[] sources = myForm.getSourceId().split("_");
		
		DBAccessor dbAccessor = DBServer.newDBAccessor(true);
		DepartmentDAO departmentDAO = new DepartmentDAO(dbAccessor);
		List<Department> list = departmentDAO.getDepartmentsByParentId(Integer.parseInt(sources[1]), AppUtils.getFieldWorkId());
		
		UserDAO userDAO = new UserDAO(dbAccessor);
		List<User> userList = userDAO.getUsersByDeptIdAndRoleId(Integer.parseInt(sources[1]), AppUtils.getFieldWorkId());
		dbAccessor.close();
		
		StringBuffer buffer = new StringBuffer(512);
		buffer.append("{result:[");
		
		for (Department department : list)
		{
			buffer.append("{id:'" + Constants.WAREHOUSE_TYPE_ORG + "_" + department.getDept_id() + "', text:'" + department.getDept_name());
			buffer.append("', realId:'" + department.getDept_id() + "', type:'" + Constants.WAREHOUSE_TYPE_ORG + "', href:'#', title:'', hasChild:");
			buffer.append(department.getChild_count() > 0 ? "true" : "false");
			buffer.append(", childs:[]},");
		}
		
		for (User user : userList)
		{
			buffer.append("{id:'" + Constants.WAREHOUSE_TYPE_USER + "_" + user.getUser_id() + "', text:'" + user.getUser_name());
			buffer.append("', realId:'" + user.getUser_id() + "', type:'" + Constants.WAREHOUSE_TYPE_USER + "', href:'#', title:'', hasChild:");
			buffer.append("false");
			buffer.append(", childs:[]},");		
		}
		
		if (!list.isEmpty() || !userList.isEmpty())
		{
			buffer.deleteCharAt(buffer.length()-1);
		}
		
		buffer.append("]}");
		response.setContentType("text/html;charset=gbk");
		PrintWriter writer = response.getWriter();
		writer.print(buffer.toString());
		writer.flush();
		writer.close();
		return null;
	}
}
